#include "DvpCamera_class.h"

DvpCamera_class::DvpCamera_class(string mode = "MAX", double exp_time = 10000.0, float gain = 1.0)
{
	dvpUint32 count;//设备个数
	dvpCameraInfo info[8];//相机信息
	dvpUint32 i = 0;//分辨率选择

	/* 更新设备列表 */
	dvpRefresh(&count);
	if (count > 8) count = 8;
	if (count == 0) exit(0);

	for (int i = 0; i < count; i++)
	{
		/*枚举相机信息*/
		if (dvpEnum(i, &info[i]) == DVP_STATUS_OK)
		{
			printf("Camera FriendlyName : %s\r\n", info[i].FriendlyName);
		}
	}
	cout << "lolololoo" << endl;
	/*根据友好名称打开相机设备 */
	status = dvpOpenByName(info[0].FriendlyName, OPEN_NORMAL, &h);
	if (status != DVP_STATUS_OK)
	{
		printf("status: %d dvpOpenByName failed!!!\n", status);
		exit(0);
	}
	/* Grab frames for every preset ROI */
	dvpStreamState state;//获取视频流状态
	dvpGetStreamState(h, &state);
	if (state == STATE_STOPED)
	{
		status = dvpStart(h);//启动视频流
		if (status != DVP_STATUS_OK)
		{
			printf("status: %d start frame failed!!!\n", status);
			exit(0);
		}
	}
	cout << "lalala" << endl;
	/*@@Set quickly camera model */
	if (mode == "MAX") i = 0;
	else if (mode == "ROI") i = 1;
	else if (mode == "BIN") i = 2;
	status = dvpSetQuickRoiSel(h, i);
	printf("dvpSetQuickRoiSel %d\n", i);
	if (status != DVP_STATUS_OK)
	{
		printf("dvpSetQuickRoiSel failed with err : %d\r\n", status);
	}

	/*@@Set exposure*/
	status = dvpSetAeMode(h, AE_MODE_AE_ONLY);//设置只自动曝光模式
	if (status != DVP_STATUS_OK)
	{
		printf("dvpSetAeMode failed with err : %d\r\n", status);
	}
	status = dvpSetAeOperation(h, AE_OP_OFF);//关闭自动曝光
	if (status != DVP_STATUS_OK)
	{
		printf("dvpSetAeOperation failed with err : %d\r\n", status);
	}
	status = dvpSetAntiFlick(h, ANTIFLICK_DISABLE);//关闭抗频闪（选择直流）
	if (status != DVP_STATUS_OK)
	{
		printf("dvpSetAntiFlick failed with err : %d\r\n", status);
	}
	status = dvpSetExposure(h, exp_time);//设置曝光时间（us）
	if (status != DVP_STATUS_OK)
	{
		printf("dvpSetExposure failed with err : %d\r\n", status);
	}

	/*@@Set Analog Gain*/
	status = dvpSetAnalogGain(h, gain);// 设置模拟增益的值
	if (status != DVP_STATUS_OK)
	{
		printf("dvpSetAnalogGain failed with err : %d\r\n", status);
	}
	status = dvpSetRgbGain(h, 1.000, 1.250, 1.000);//设置红,绿,蓝三色的数字增益
	if (status != DVP_STATUS_OK)
	{
		printf("dvpSetRgbGain failed with err : %d\r\n", status);
	}
}

DvpCamera_class::~DvpCamera_class()
{
}

DvpCamera_class & DvpCamera_class::operator>>(cv::Mat & img)
{
	dvpFrame frame;
	void *p;
	do
	{
		status = dvpGetFrame(h, &frame, &p, 1000);
		if (status != DVP_STATUS_OK)
		{
			printf("get frame failed!!!  err:%d\r\n", status);
			if (status != -1000)
				exit(0);
		}
		else
			break;
	} while (status == -1000);
	//img = cv::Mat(frame.iHeight, frame.iWidth, CV_8UC3, p);
	Mat image(frame.iHeight, frame.iWidth, CV_8UC3, p);
	img = image.clone();
	// TODO: 在此处插入 return 语句
}
